const util = require('../../../util/util')
const DAL = require('./DAL')
const result = {
  msg: 'mark BLL [not run]',
  data: null,
  code: 40001
}
function formatResult(res) {
  if (res.msg == 'success') {
    res.code = 10000
  } else {
    res.data = null
    res.code = 40001
  }
  return res
}
module.exports = {
  // 查询记录点
  async getMarks( ctx ) {
    let res = result
    let q = ctx.request.query
    q.sTime = q.sTime ? util.formatGmt(q.sTime,'-') : new Date().zoneDate('2022-12-01 00:00:00')
    q.eTime = q.eTime ? util.formatGmt(q.eTime,'-') : new Date().zoneDate()
    if (!q.openId) {
      res.msg = '无效的查询目标ID'
    } else if (q.sTime == 'Invalid Date' || q.eTime == 'Invalid Date') {
      res.msg = '日期格式错误'
    } else {
      res = await DAL.getMarks(q)
    }
    res = formatResult(res)
    ctx.body = res
  },
  // 按id查询记录点
  async getMarksById(ctx) {
    let res = result
    let q = ctx.request.query
    if (q.recIds) q.recIds = JSON.parse(q.recIds)
    if (!q.openId) {
      res.msg = '无效的查询目标ID'
    } else {
      res = await DAL.getMarksById(q)
    }
    res = formatResult(res)
    ctx.body = res
  },
  // 创建记录点
  async createMark( ctx ) {
    let res = result
    let b = ctx.request.body
    if (!b.openId) {
      res.msg = '无效的操作员信息'
    } else if (!b.lat || !b.lng) {
      res.msg = '请输入正确的经纬度'
    } else {
      let mtStr = b.markTime?util.formatGmt(b.markTime,'-'):util.formatDate(new Date().zoneDate(),'-')
      b.markTime = b.markTime?new Date().zoneDate(b.markTime):new Date().zoneDate()
      b.mem = '预留备注'
      // 查询某地点当天的打卡记录
      let q = {}
      q.openId = b.openId
      q.sTime = mtStr.split(' ')[0]+' 00:00:00'
      q.eTime = mtStr.split(' ')[0]+' 23:59:59'
      q.lat = b.lat
      q.lng = b.lng
      q.mtSort = -1
      let cr = await DAL.getMarks(q)
      let tDiff = 99
      for(let i in cr.data) {
        // console.log(b.markTime+':::'+cr.data[i].markTime)
        if (i == 0) {
          tDiff = parseInt(b.markTime - cr.data[i].markTime) / 1000 / 60 / 60
        }
      }
      // console.log(tDiff)  // 同一地点当前时间与最后一次打卡时间之差大于2小时才可再次打卡
      if (Math.abs(tDiff) > 2) {
        res = await DAL.createMark(b)
      } else {
        res.msg = '同一地点2小时内只能打卡一次'	
      }
      res = formatResult(res)
    } 
    ctx.body = res
  },
  // 设置记录点 （删除、删除图片、设置禁止显示、设置禁止显示图片、修改图片）
  async setMark( ctx ) {
    let res = result
    let b = ctx.request.body
    if (!b.openId) {
      res.msg = '无效的操作员信息'
    } else if (!b.recId) {
      res.msg = '无效的打卡记录Id'
    } else {
      if (b.flag == 'DEL_MARK') res = await DAL.delMark(b)
      else if (b.flag == 'DEL_IMG') res = await DAL.delMarkImg(b)
      else if (b.flag == 'HIDE_MARK' || b.flag == 'HIDE_IMG' || b.flag == 'UPDATE_IMG') res = await DAL.setMark(b)
      else res.msg = '无效的操作请求'
      res = formatResult(res)
    } 
    ctx.body = res
  },
  // 按openid查询所有记录日期
  async getMarksDate( ctx ) {
    let res = result
    let q = ctx.request.query
    if (!q.openId) {
      res.msg = '无效的查询目标ID'
    } else {
      res = await DAL.getMarksDate(q)
    }
    res = formatResult(res)
    ctx.body = res
  },
}